package pers.rain.WebMagicDemo.PageProcessor;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import pers.rain.WebMagicDemo.Bean.Books;
import pers.rain.WebMagicDemo.Dao.BooksDao;
import pers.rain.WebMagicDemo.Dao.impl.BooksDaoImpl;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.ConsolePipeline;
import us.codecraft.webmagic.pipeline.FilePipeline;
import us.codecraft.webmagic.processor.PageProcessor;

public class ToutiaoPageProcessor implements PageProcessor {

	private Site site = Site.me().setRetryTimes(4).setSleepTime(100);

	private static String DoubanURL = "https://book.douban.com/";
	private static String DoubanXpath = "(https://book\\.douban\\.com/)";
	private static String ToutiaoURL = "http://www.toutiao.com/";

	@Override
	public Site getSite() {
		// TODO Auto-generated method stub
		return site;
	}

	@Override
	public void process(Page page) {
		// TODO Auto-generated method stub
		page.putField("title", page.getHtml().xpath("//h4[@class='title']/text()"));
		page.putField("author", page.getHtml().xpath("//span[@class='author']/text()"));
		page.putField("year", page.getHtml().xpath("//span[@class='year']/text()"));
		page.putField("publisher", page.getHtml().xpath("//span[@class='publisher']/text()"));
		page.putField("desc1", page.getHtml().xpath("//p[@class='abstract']/text()"));
		page.addTargetRequests(page.getHtml().links().regex(DoubanXpath).all());
		// System.out.println("content:"+page.getHtml());
		Map<String, String> mapList = new HashMap<String, String>();
		List<Books> bookList = new ArrayList<Books>();
		// ResultItems resultItems = page.getResultItems();
		SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM");
		Books books = null;
		BooksDao bookDao = null;
		for (int i = 1; i <= 4; i++) {
			books = new Books();
			bookDao = new BooksDaoImpl();
			mapList.put("title", page.getHtml()
					.xpath("//div[@class='slide-list']/ul[2]/li[" + i + "]//h4[@class='title']/text()").toString());
			mapList.put("author", page.getHtml()
					.xpath("//div[@class='slide-list']/ul[2]/li[" + i + "]//span[@class='author']/text()").toString());
			mapList.put("year", page.getHtml()
					.xpath("//div[@class='slide-list']/ul[2]/li[" + i + "]//span[@class='year']/text()").toString());
			mapList.put("publisher",
					page.getHtml()
							.xpath("//div[@class='slide-list']/ul[2]/li[" + i + "]//span[@class='publisher']/text()")
							.toString());
			mapList.put("desc1", page.getHtml()
					.xpath("//div[@class='slide-list']/ul[2]/li[" + i + "]//p[@class='abstract']/text()").toString());
			books.setTitle(page.getHtml()
					.xpath("//div[@class='slide-list']/ul[2]/li[" + i + "]//h4[@class='title']/text()").toString());
			books.setAuthor(page.getHtml()
					.xpath("//div[@class='slide-list']/ul[2]/li[" + i + "]//span[@class='author']/text()").toString());
			try {
				books.setYear(sim.parse(mapList.get("year")));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			books.setPublisher(mapList.get("publisher"));
			books.setDesc(page.getHtml()
					.xpath("//div[@class='slide-list']/ul[2]/li[" + i + "]//p[@class='abstract']/text()").toString());
			bookList.add(books);
			// System.out.println("before: "+books);

			bookDao.insertBook(books);// insert into mysql
			// System.out.println("query: "+bookDao.queryBook(1));

		}
		System.out.println("bookList: " + bookList);

		/*
		 * System.out.println("**************"+resultItems+"**************\n");
		 * 
		 * System.out.println("title: "+resultItems.get("title"));
		 * System.out.println("author: "+resultItems.get("author"));
		 * System.out.println("year: "+resultItems.get("year"));
		 * System.out.println("publisher: "+resultItems.get("publisher"));
		 * System.out.println("desc: "+resultItems.get("desc"));
		 */

	}

	public static void main(String[] args) {
		// Spider.create(new
		// ToutiaoPageProcessor()).addUrl(DoubanURL).addPipeline(new
		// ConsolePipeline()).thread(1).run();

		Spider.create(new ToutiaoPageProcessor()).addUrl(DoubanURL)
				.addPipeline(new FilePipeline("C:\\Users\\rain\\Desktop\\output")).thread(1).run();

		// books.setAuthor();

		System.out.println("Terminate.\n");
	}
}
